package www.core.com.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import www.core.com.dao.BaseDAO;
import www.core.com.pojo.Group;
import www.core.com.pojo.Label;
import www.core.com.pojo.Message;
import www.core.com.pojo.MessageSys;
import www.core.com.pojo.User;
import www.core.com.service.ILabelService;
@Transactional
@Service("labelService")
public class LabelServiceImpl implements ILabelService {
	@Resource(name = "hibernateDAO")
	BaseDAO<Label> hibernateDAO;

	
	@Override
	public Label labelExistence(Label label) {
		label.setName(label.getName().toLowerCase());
		Map< String, String> map = new HashMap<String,String>();
		map.put("name", label.getName());
		Label result = hibernateDAO.findByColumn("from "+Label.class.getSimpleName()+" where lower(name)=:name ", map);
		return result;
	}
	
	public Label labelExistenceLock(Label label) {
		Label result = hibernateDAO.jdbcSelectLockRetunBean("select * from syslabel where lower(name)='"+label.getName().toLowerCase()+"' for update",Label.class);
		
		return result;
	}
	
	@Override
	public Label addNewLable(Label label) {
		hibernateDAO.saveOrUpdate(label);
		return label;
	}
	@Override
	public void labelDeleteGroupDeleteLock(Integer id) {
		// TODO Auto-generated method stub
		Label result = hibernateDAO.jdbcSelectLockRetunBean("select * from syslabel where id="+id+" for update",Label.class);
		if(result !=null){
			result.setFoucsGroup(result.getFoucsGroup()-1);
			hibernateDAO.saveOrUpdate(result);
		}
		
	}
	
	
}
